package com.sakura.mall.product.categoryBrandRelation.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.categoryBrandRelation.domain.entity.CategoryBrandRelationEntity;
import com.sakura.mall.product.categoryBrandRelation.domain.request.AddCategoryBrandRelationRequest;
import com.sakura.mall.product.categoryBrandRelation.domain.request.UpdateCategoryBrandRelationRequest;
import com.sakura.mall.product.categoryBrandRelation.domain.request.CategoryBrandRelationPageRequest;
import com.sakura.mall.product.categoryBrandRelation.domain.vo.CategoryBrandRelationVo;
import com.sakura.mall.product.categoryBrandRelation.mapper.CategoryBrandRelationMapper;
import com.sakura.mall.product.categoryBrandRelation.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 品牌分类关联 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddCategoryBrandRelationRequest request) {
        // todo 其他校验逻辑
        CategoryBrandRelationEntity categoryBrandRelation = request.convert(CategoryBrandRelationEntity.class);
        this.save(categoryBrandRelation);
    }

    @Override
    public Page<CategoryBrandRelationVo> queryPage(CategoryBrandRelationPageRequest request) {
        IPage<CategoryBrandRelationEntity> qp = request.toPage();
        LambdaQueryWrapper<CategoryBrandRelationEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<CategoryBrandRelationVo> ret = qp.convert(entity -> entity.convert(CategoryBrandRelationVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateCategoryBrandRelationRequest request) {
        CategoryBrandRelationEntity entity = request.convert(CategoryBrandRelationEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

